package com.clps.algorithm03.队列;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Queue2 <E>{
    private final Object[] data; // 队列中存放元素的数组
    private final int maxSize;// 队列的最大容量
    private int size; // 队列中元素的个数
    private int head = 0;// 队列的头，只能删除，指向队列的第一个元素
    private int tail = 0; // 队列的尾，只能添加，指向队列最后一个元素的下一个位置


    public Queue2(int maxSize) {
        if (maxSize <= 0)
            throw new IllegalArgumentException("队列容量必须大于0 : " + maxSize);
        this.maxSize = maxSize;
        data = new Object[this.maxSize];
    }
    
    //这里可以扩容可以抛出异常,为了方便这里目前就不扩容了,关于扩容,大家
    public void  add(E val){
        if(isFull()){
            throw new IllegalArgumentException("队列已将满了,无法再加入...");
        }
        data[tail++]=val;
        size++;
    }


    // 删除
    public E remove() {
        if (isEmpty())
            throw new IllegalStateException("队列是空的，无法删除……");
        E res = (E) data[head];
        data[head++] = null;
        size--;
        return res;
    }
    private boolean isFull() {
        return  tail== maxSize;
    }

    public boolean isEmpty() {
        return size==0;
    }


    public  int size(){
        return size;
    }

}
